/**
 * @param {number[]} people
 * @param {number} limit
 * @return {number}
 */
 var numRescueBoats = function(people, limit) {
  people.sort((a,b)=>b-a) // 从大到小排序
  console.log(people)
  let left = 0
  let right = people.length-1 // 左闭右闭区间
  let count = 0 // 救生艇数量
  while(left <= right){
      if(people[left] + people[right] > limit){
          // 如果头加尾大于limit 那么只能有一个头
          count++
          left++
      }else if (people[left] + people[right] == limit){
          // 如果相等 也是一个救生艇
          count++
          left++
          right--
      }else{
          let sum = people[left] + people[right]
          while(sum < limit && left < right){
              right--
              sum += people[right]
          }
          count++
          left++
      }
  }
  return count
};
numRescueBoats([2,49,10,7,11,41,47,2,22,6,13,12,33,18,10,26,2,6,50,10],50)